package com.ruoyi.web.rabbitmq;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.ruoyi.web.controller.dto.OrderTest;
import com.ruoyi.web.controller.dto.OrderVo;
import com.ruoyi.web.webSocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Component
@Slf4j
public class OrderListener {
    @Autowired
    private WebSocketServer webSocketServer;


    @RabbitHandler
    @RabbitListener(queues = "2010a")
    public void saveOrder(OrderVo orderVo, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG)Long id ) throws IOException {

        String string = JSON.toJSONString(orderVo);

        webSocketServer.sendAllMessage(string);

        channel.basicAck(id,false);
    }

}
